cc.Class({
    extends: cc.Component,

    properties: {
        motorBox: cc.Node,
        motorType: cc.Sprite,
        motorRole: dragonBones.ArmatureDisplay,
        motorDetail: cc.Button,
        motorName: cc.RichText,
        motorOdds: cc.Label,
        motorState: cc.ProgressBar,
        motorBet: cc.Button,
        motorOil: require('MotorOil'),
        typeSpts: [cc.SpriteFrame],


        lb_reward: cc.Label,
        lb_sex: cc.Label,
    },

    onLoad() {
        this.motorId = 0;
        this.isAnimating = false;
        this.find('touch').on('click', this.onClick, this);
        this.motorRole.addEventListener('complete', () => {
            this.isAnimating = false;
            this.motorRole.playAnimation('正常前进');
        }, this);
        this.initWords();
    },
    initWords: function(){
        this.lb_reward.string = LangLocal.word8;
        this.lb_sex.string = LangLocal.word9;
    },

    onClick() {
        this.node.emit('click');
        this.motorBox.stopAllActions();
        this.motorBox.runAction(cc.sequence(
            cc.scaleTo(0.2, 1.06).easing(cc.easeSineOut()),
            cc.scaleTo(0.1, 1).easing(cc.easeSineIn())
        ));
        var oil = this.node.find('oil_panel');
        oil.stopAllActions();
        oil.runAction(cc.sequence(
            cc.scaleTo(0.2, 1.1).easing(cc.easeSineOut()),
            cc.scaleTo(0.1, 1).easing(cc.easeSineIn())
        ));
    },

    init(data) {
        this.motorId = data.order + 1;
        if(data.order % 2 == 1) {
            this.motorBox.rotation = 180;
            this.motorBox.x = -20;
        }
        this.motorName.string = '<b><color=#0008a6a>00' + this.motorId + '</color></b>';
        this.motorType.spriteFrame = this.typeSpts[data.order];
        this.motorRole.armatureName = data.role;
        this.motorDetail.node.on('click', () => cc.Popup.show('MotorsPop', pop => pop.switchInfo(data.order)), this);
    },

    updateData(data) {
        this.motorOdds.string = 'x' + data.odds.toFixed(2);
        this.motorState.progress = data.rate;
    },

    modify(count) {
        this.motorOil.modify(count);
        if(!this.isAnimating) {
            this.isAnimating = true;
            this.motorRole.playAnimation('加速后_冲刺', 1);
        }
    },

    stopGuide() {
        this.guideNode && this.node.removeChild(this.guideNode);
        this.guideNode = null;
    },

    playGuide(guide) {
        this.guideNode = guide;
        this.guideNode.parent = this.node;
        this.guideNode.position = this.node.find('oil_panel').position;
        this.guideNode.y -= 6;
    }

});
